package other;
//发邮件的代码例子

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.email.EmailWorkRunnable;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;


/**
 *
 * 内容：监控模具履历台账表累计生产模次数量超设计寿命数量90%,发邮件提醒对应负责人【沈军、
 成基斌、徐健】
 * 作者：万超
 * 版本：V1.0
 * 时间：2025-03-14 上午11:33:27
 *
 */
public class AutoSendMailBySYSCMC extends BaseCronJob{

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        BaseBean log = new BaseBean();
        RecordSet rs = new RecordSet();
        String sql_query = "SELECT mjbh,cpmc,lh,sjsm,ljscmc,sysymc,(to_char(ROUND((sysymc/sjsm)*100,2),'fm9999990.00')||'%') warn FROM uf_MouldHistoryInfo WHERE sjsm IS NOT NULL AND (sysymc/sjsm)<0.1";
        rs.executeQuery(sql_query);
        //无预警数据
        if(rs.getCounts()==0) {

            return ;
        }


        //邮件内容格式化
        String beginCN_str = "以下模具【剩余使用模次】已达【设计寿命】的最大预警值，请及时处理!";

        //开始拼接table
        StringBuilder content = new StringBuilder("<html><head></head><body><div>");
        content.append("<div><p>"+beginCN_str+"</p></div>");
        content.append("<br/>");
        content.append("<table border=\"1\"  style=\"border:solid 1px #E8F2F9;font-size:18px;\">");
        //text-align: center;
        content.append("<tbody>");
        content.append("<tr  style=\"background-color: #428BCA; color:#ffffff\">");
        content.append("<th width=15%>模具编号</th>");
        content.append("<th width=20%>产品名称</th>");
        content.append("<th width=20%>料号</th>");
        content.append("<th width=12%>设计寿命</th>");
        content.append("<th width=12%>累计生产模式</th>");
        content.append("<th width=12%>剩余使用模次</th>");
        content.append("<th width=9%>预警值</th></tr>");
        //content.append("<th width=5%>完成度</th>");
        //content.append("<th width=8%>状态</th>");



        while(rs.next()) {
            //当前行数据

            content.append("<tr><td>"+Util.null2String(rs.getString("mjbh"))+"</td>");
            content.append("<td>"+Util.null2String(rs.getString("cpmc"))+"</td>");
            content.append("<td>"+Util.null2String(rs.getString("lh"))+"</td>");
            content.append("<td style=\"text-align: center;\">"+ Util.null2String(rs.getString("sjsm"))+"</td>");
            content.append("<td style=\"text-align: center;\">"+Util.null2String(rs.getString("ljscmc"))+"</td>");
            content.append("<td style=\"text-align: center;\">"+Util.null2String(rs.getString("sysymc"))+"</td>");
            content.append("<td style=\"text-align: center;\">"+Util.null2String(rs.getString("warn"))+"</td></tr>");

        }


        String end_str = "<span>请查看URL链接:</span><a href = 'http://oa.recodeal.com:8899/spa/cube/index.html#/main/cube/search?customid=130501'>点击进入</a>";

        String emailTitle = "模具使用模次预警通知";
        //String emailAddress = getEmailByUserid(key);
        String ccAddress = "chao.wan@recodeal.com,qs@recodeal.com";
        String emailAddress = "jun.shen@recodeal.com,jibin.cheng@recodeal.com,jian.xu@recodeal.com";
        content.append("</tbody></table></div>");
        //content.append("<div style=\"float:right;margin-top:50px\">"+end_str);
        content.append("<br/>");
        content.append("<div>"+end_str);
        content.append("</div></div></body></html>");

        EmailWorkRunnable ewr = new EmailWorkRunnable(emailAddress,ccAddress,"",emailTitle,content.toString());
        //参数  : 收件人地址 , 抄送地址 , "密送地址(能收到,但邮件上看不到这个人)" ,邮件标题 , 邮件内容
        boolean emailSendResult = ewr.emailCommonRemind();

        if(!emailSendResult) {
            log.writeLog("发送邮件失败! 当前邮件对应人员:"+";邮箱地址:"+emailAddress);
        }else {
            log.writeLog("发送邮件成功!");
        }

    }

}

